package com.egg.common.translate.enums;


import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

@Getter
@AllArgsConstructor
public enum I18nTypeEnum {


    ZH_CH("中国-大陆", "zh_cn"),
    EN_US("英文-美国", "en_us"),

    ;


    private String message;
    private String code;


    public static Optional<I18nTypeEnum> findByCode(String code) {
        if (ObjectUtil.isEmpty(code)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(o -> Objects.equals(code, o.getCode())).findAny();
    }

    public static Optional<Locale> findLocaleByCode(String code) {
        Optional<I18nTypeEnum> opt = findByCode(code);
        return opt.map(i18nTypeEnum -> new Locale(i18nTypeEnum.getCode()));
    }

    public static I18nTypeEnum defaultType() {
        return I18nTypeEnum.ZH_CH;
    }

    public static Locale defaultLocale() {
        return new Locale(defaultType().code);
    }


}